<script setup lang="ts">
import lodash from 'lodash'
import {getStorage,saveStorage} from '@/api/storage'
import {choosePath} from '@/api/general'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emits = defineEmits(['close'])
const loading = ref<boolean>(false)
const show = ref<boolean>(false)
const dataForm = ref<any>({
    hospitalName:undefined,
    filePath:undefined,
    dataPath:undefined
})

const domForm = ref()

//获取数据的方法
const getData = () => {
    getStorage().then((e:any)=>{
        dataForm.value = e.value
    })
}

const handleClose = (refresh?:boolean) => {
    emits('close',{refresh})
}

const handleSave = () => {
    domForm.value.validate((vaild:boolean)=>{
        if(!vaild) return
        loading.value = true
        saveStorage(lodash.cloneDeep(dataForm.value)).then((e:any)=>{
            const { cancel } = e??{}
            loading.value = false
            if(cancel){
                proxy?.$modal.alertWarning('已取消应用设置')
                getData()
                return
            }
            proxy?.$modal.alertSuccess('保存成功',()=>{
                handleClose(true)
            })
        })
    })
}

const handleChoosePath = (mark:string)=>{
    choosePath(mark).then((e:any)=>{
        const {mark,value} = e
        switch(mark){
            case 'file':
                dataForm.value.filePath = value
                break
            case 'data':
                dataForm.value.dataPath = value
                break
        }
    })
}

onMounted(()=>{
    getData()
})

show.value = true
</script>
<template>
	<el-dialog v-model="show" title="本机设置" :close-on-click-modal="false" :show-close="false" width="500" destroy-on-close>
		<el-form ref="domForm" :model="dataForm" label-width="auto" style="max-width: 600px" v-loading="loading">
			<el-form-item label="机构名称" prop="hospitalName" required>
				<el-input v-model="dataForm.hospitalName" placeholder="请输入机构名称" />
			</el-form-item>
			<el-form-item label="影像目录" prop="filePath" required>
				<el-input v-model="dataForm.filePath" placeholder="请选择影像文件夹路径" readonly>
					<template #append>
						<el-button icon="Pointer" @click="()=>handleChoosePath('file')" />
					</template>
				</el-input>
			</el-form-item>
			<el-form-item label="数据目录" prop="dataPath" required>
				<el-input v-model="dataForm.dataPath" placeholder="请选择数据文件夹路径" readonly>
					<template #append>
						<el-button icon="Pointer" @click="()=>handleChoosePath('data')" />
					</template>
				</el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="handleClose" :loading="loading">关闭</el-button>
				<el-button type="primary" @click="handleSave" :loading="loading">保存</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<style lang="scss"></style>
<style lang="scss" scoped></style>
